package com.deanlib.cabinet.base;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.deanlib.cabinet.data.network.Response;
import com.deanlib.plantern.utils.AppUtils;
import com.elvishew.xlog.XLog;

import java.util.Optional;

import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

public abstract class BaseActivity<V extends ViewDataBinding> extends AppCompatActivity/*RxAppCompatActivity*/
        implements IUIAuxiliary,IPopupAuxiliary,INetworkAuxiliary,IActionProtocol {

    protected V binding;
    protected NetworkAuxiliary networkAuxiliary;
    private PopupAuxiliary popupAuxiliary;
    private CompositeDisposable compositeDisposable;

    public static boolean exited = false;

    public static String TAG = "BaseActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TAG = getClass().getSimpleName();
        int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            attributes.flags |= flagTranslucentNavigation;
            window.setStatusBarColor(Color.TRANSPARENT);
        } else {
            attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
        }
        window.setAttributes(attributes);
        AppUtils.setSystemAppbarTranslucent(this, true);


//        setContentView(getLayoutId());
        binding = DataBindingUtil.setContentView(this, getLayoutId());
        popupAuxiliary = new PopupAuxiliary(binding, this);
        networkAuxiliary = new NetworkAuxiliary(popupAuxiliary);
        init(savedInstanceState);
    }

    @Override
    public void init(Bundle savedInstanceState) {
        //默认调用流程，但很多情况下并不是这个调用顺序
        //可以覆写 Init方法
        initData(savedInstanceState);
        initView();
        loadData();
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        checkLoggingStatus();
    }

    //检查登录状态
    private void checkLoggingStatus(){
        if (exited){

            /*if (this instanceof LaunchActivity ||
                    this instanceof LoginActivity){
                //不需要授权的 activity 页面

            }else {
                finish();
            }*/
        }
    }

    protected void doLogin(){
        exited = false;
    }

    protected void doLogout(){
        exited = true;
        checkLoggingStatus();
    }

    @Override
    public void toast(String msg) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.toast(msg);
        }
    }

    @Override
    public void loading(String title, String content) {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loading(title, content);
        }
    }

    @Override
    public void loaded() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.loaded();
        }
    }

    @Override
    public void networkError() {
        if (popupAuxiliary !=null) {
            popupAuxiliary.networkError();
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next);
        }else {
            XLog.e( "NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public <T> void request(@NonNull Observable<Response<T>> observable, Consumer<Optional<T>> next, Consumer<? super Throwable> error, Action complete) {
        if (networkAuxiliary !=null){
            networkAuxiliary.request(observable, next, error, complete);
        }else {
            XLog.e("NetworkAuxiliary is null");
        }
    }

    @Override
    public void dispose() {
        if (networkAuxiliary !=null){
            networkAuxiliary.dispose();
        }
    }

    protected void addDisposable(Disposable disposable){
        if (compositeDisposable == null){
            synchronized (this){
                if (compositeDisposable == null){
                    compositeDisposable = new CompositeDisposable();
                }
            }
        }
        compositeDisposable.add(disposable);
    }

    @Override
    protected void onDestroy() {
        dispose();
        if (compositeDisposable!=null) {
            compositeDisposable.dispose();
        }
        super.onDestroy();
    }
}
